package com.ljm.util;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.time.LocalDate;

public class ImageUtil {
    static File filePath = new File("E://img//images");
    static String date = LocalDate.now().toString();
    public static void main(String[] args) throws Exception {
            Document document = Jsoup.connect("https://bing.ioliu.cn/?p=1").get();
            Elements elements = document.select("a.mark");
//            for(Element element:elements){
//                System.out.println(element.toString());
//            }
                String href = elements.get(0).attr("href");
                String newURL = "https://bing.ioliu.cn/" + href + "";
                Document document1 = Jsoup.connect(newURL).get();
                String imageSrc = document1.select("img.target.progressive__img.progressive--not-loaded").attr("data-progressive");
                System.out.println(imageSrc);
                try {
                    byte[] bytes = Jsoup.connect(imageSrc).ignoreContentType(true).execute().bodyAsBytes();
                    FileOutputStream fileOutputStream = new FileOutputStream(filePath+"/"+date+".jpg");
                    fileOutputStream.write(bytes);

                } catch (Exception e) {
                    System.out.println("failed!");

                }
        System.out.println("done!");
    }

    public  File getFilePath() {
        return filePath;
    }

    public  String getDate() {
        return date;
    }
}



